﻿using System;
using System.Collections;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;
using _3D.Data;

namespace _3D
{
    public class myPictureBox : System.Windows.Forms.UserControl
    {
        #region Initialization

        private System.ComponentModel.Container components = null;
        public myPictureBox()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code
       
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // myPictureBox
            // 
            this.Name = "myPictureBox";
            this.Size = new System.Drawing.Size(153, 128);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.myPictureBox_Paint);
            this.ResumeLayout(false);

        }
        #endregion
        #endregion

        private Image TheImage = null;
        private int[,] mazePath;
        private int xSize, ySize, minxSyS, penSize;
        private int X = 0;
        private int Y = 0;   // coordiantes
        private int msx, msy;
        private Game game;

        public void SetGame(ref Game _game, int _msx, int _msy)
        {
            game = _game;
            msx = _msx; msy = _msy;
            xSize = this.Image.Width / _msx;
            ySize = this.Image.Height / _msy;
            minxSyS = (xSize > ySize ? ySize : xSize);
            penSize = (minxSyS > 16 ? minxSyS / 16 : 2);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            Focus();
            Pen p = new Pen(Color.Red, penSize);
            Graphics g = Graphics.FromImage((Image)this.Image);

            if (X < 0) { X = 0; }
            if (X > msx) { X = msx; }
            if (Y < 0) { Y = 0; }
            if (Y > msy) { Y = msy; }

            switch (e.KeyCode)
            {
                case Keys.W:
                    p.Color = Color.Khaki;
                    g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);

                    if (game.CheckConnect(X, Y, "north") == true)
                    {
                        Y--;
                        p.Color = Color.Red;
                        g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);
                    }
                    else
                    {
                        p.Color = Color.Red;
                        g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);
                    }

                    break;
                case Keys.S:
                    p.Color = Color.Khaki;
                    g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);

                    if (Y == msy)
                        { Y--; }
                    if (game.CheckConnect(X, Y, "south") == true)
                    {
                        Y++;
                        p.Color = Color.Red;
                        g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);
                    }
                    else
                    {
                        p.Color = Color.Red;
                        g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);
                    }

                    break;
                case Keys.A:
                    p.Color = Color.Khaki;
                    g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);

                    if (game.CheckConnect(X, Y, "west") == true)
                    {
                        X--;
                        p.Color = Color.Red;
                        g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);
                    }
                    else
                    {
                        p.Color = Color.Red;
                        g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);
                    }

                    break;
                case Keys.D:
                    p.Color = Color.Khaki;
                    g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);

                    if (X == msx)
                        { X--; }
                    if (game.CheckConnect(X, Y, "east") == true)
                    {
                        X++;
                        p.Color = Color.Red;
                        g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);
                    }
                    else
                    {
                        p.Color = Color.Red;
                        g.DrawRectangle(p, xSize * X + xSize / 2, ySize * Y + ySize / 2, 1, 1);
                    }

                    break;
                default:
                    break;
            }

            p.Dispose();
            g.Dispose();
            this.Refresh();

            if (Y == msy-1 && X == msx-1)
            {
                MessageBox.Show("You win!","Congratulations!");
            }
        }

        public int cX
        {
            get
            { return X; }
            set
            { X = value; }
        }

        public int cY
        {
            get
            { return Y; }
            set
            { Y = value; }
        }

        public Image Image
        {
            get
            { return TheImage; }
            set
            { TheImage = value; }
        }

        private void myPictureBox_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Brush b = new SolidBrush(SystemColors.Control);

            g.FillRectangle(b, this.ClientRectangle);
            if (TheImage != null)
            {
                g.DrawImageUnscaled(TheImage, 0, 0, TheImage.Width, TheImage.Height);
                g.FillRectangle(b, ClientRectangle.Width, ClientRectangle.Height, 512, 512);
            }
            b.Dispose();
        }
    }
}